package com.yt.al.service;

import io.appium.java_client.AppiumDriver;

import java.util.HashMap;

import org.apache.log4j.Logger;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebElement;

import com.yt.al.util.TestUtil;
import com.yt.al.util.WriteLogUtil;

public class BaseService {
	AppiumDriver driver;
	
	public BaseService()
	{
		
	}
	
	public void init(AppiumDriver _driver)
	{
		driver = _driver;
	}
	
	/**
	 *点击元素
	 * 
	 * @param webElement
	 * @throws Exception 
	 */
	public void clickDeal(WebElement ele) throws Exception {
		if (ele != null) {
			try {
				ele.click();
			} catch (Exception e) {
				WriteLogUtil.writeMsgToFile("click:" + e);
			}
		} else {
			throw new Exception("没有找到要点击的元素");
		}
	}

	/**
	 *向 指定元素 输入指定字符
	 * 
	 * @param webElement
	 * @param WriteContext
	 * @throws Exception 
	 *        
	 */
	public void writeDeal(WebElement webElement, String WriteContext) throws Exception {
		String text = getTextDeal(webElement);
		if(text!=null && !text.equals(""))
		{
			clearDeal(webElement);
		}
		sendKeysDeal(webElement, WriteContext);
	}


	/**
	 * sendKey
	 * 
	 * @param webElement
	 * @param text
	 * @throws Exception 
	 */
	public void sendKeysDeal(WebElement webElement, String text) throws Exception {
		if (webElement != null) {
			try {
				webElement.sendKeys(text);
			} catch (Exception e) {
				throw new Exception("sendKeys" + e);
			}
		} else {
			throw new Exception("没有找到元素");
		}
	}

	/**
	 * clearDeal
	 * 
	 * @param webElement
	 * @throws Exception 
	 */
	public void clearDeal(WebElement webElement) throws Exception {
		if (webElement != null) {
			try {
				webElement.clear();
			} catch (Exception e) {
				throw new Exception("clear 元素" + e.getMessage());
			}
		} else {
			throw new Exception("没有找到元素");
		}
	}

	/**
	 * ��ȡԪ�ص�����
	 * 
	 * @param webElement
	 * @return
	 * @throws Exception 
	 */
	public String getTextDeal(WebElement webElement) throws Exception {
		String text = "";
		if (webElement != null) {
			try {
				text = webElement.getText().trim();
			} catch (Exception e) {
				throw new Exception("getTextDeal" + e.getMessage());
			}
		} else {
			throw new Exception("没有元素");
		}
		return text;
	}

	/**
	 * ����
	 * 
	 * @param expandMsg
	 *            ������ʾ����Ϣ
	 * @param webElement
	 *            Ҫ����Ԫ��
	 * @param caseName
	 *            �������
	 * @throws Exception 
	 */
	public void getCheckPoint(String expandMsg, WebElement webElement,
			String caseName, StringBuffer verificationErrors) throws Exception {
		String text = getTextDeal(webElement);
		TestUtil.setPoint(expandMsg, text, caseName, verificationErrors);
	}

	public void press(int x,int y){
		try{
			driver.swipe(x, y, x, y, 5);
			
		}catch(Exception e){
			Logger.getLogger("CheckResult : ").info("��� ("+x+","+y+")����쳣");
		}
	}
	

/**
	 * 
	 * @param sx
	 * 
	 * @param sy
	 *            
	 */
	public void setPointAndFlick(Double sx, Double sy) {
		try {
			HashMap<String, Double> flickObject = new HashMap<String, Double>();
			JavascriptExecutor jse = (JavascriptExecutor) driver;
			flickObject.put("startX", sx);
			flickObject.put("startY", sy);
			flickObject.put("endX", sx - 0.01);
			flickObject.put("endY", sy);
			jse.executeScript("mobile: flick", flickObject);
		} catch (Exception e) {
			try {
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				WriteLogUtil.writeMsgToFile("����exceptionDeal�쳣" + e1);
			}
			Logger.getLogger("����setPointAndFlick�����쳣 �� ").info(e.getMessage());
		}
	}
	
	/**
	 * 水平向左滑动指定次数
	 * @param times
	 */
	public void horzlignFlickTimes(int times)
	{
		JavascriptExecutor jse = (JavascriptExecutor) driver;
		HashMap<String, Double> flickObject = new HashMap<String, Double>();
		flickObject.put("startX", 0.95);
		flickObject.put("startY", 0.5);
		flickObject.put("endX", 0.05);
		flickObject.put("endY", 0.5);
		for (int i = 1; i <= times; i++) {
			jse.executeScript("mobile: flick", flickObject);
		}
	}
	
	/**
	 * 水平向左滑动指定次数
	 * @param times
	 */
	public void verzlignFlickTimes(int times)
	{
		try
		{
			JavascriptExecutor jse = (JavascriptExecutor) driver;
			HashMap<String, Double> flickObject = new HashMap<String, Double>();
			flickObject.put("startX", 0.5);
			flickObject.put("startY", 0.55);
			flickObject.put("endX", 0.5);
			flickObject.put("endY", 0.05);
			for (int i = 1; i <= times; i++) {
				jse.executeScript("mobile: flick", flickObject);
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 
	 * 
	 * @param element
	 * @return
	 */
	public Point getLocation(WebElement element) {
		try {
			return element.getLocation();
		} catch (Exception e) {
			// TestUtil.exceptionDeal(driver, baseObject);
			return null;
		}
	}
	
	public void quit()
	{
		try
		{
			if(driver!=null)
			{
				if(driver.getSessionId()!=null)
				{
					driver.quit();
				}
			}
		}
		catch(Exception ex)
		{
			
		}
	}


}
